<template>
  <div>
    <el-table :data="tableData" border style="width: 100%" empty-text="There is currently no data available">
      <el-table-column prop="operation" label="examine and approve" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="applyAdmin(scope, 3)">Approve</el-button>
          <el-button size="mini" type="danger" @click="applyAdmin(scope, 4)">Refuse</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="uid" label="uid" width="180">
      </el-table-column>
      <el-table-column prop="username" label="username" width="180">
      </el-table-column>
      <el-table-column prop="phone" label="phone">
      </el-table-column>
      <el-table-column prop="role" label="role">
        <template slot-scope="scope">
          <span v-if="scope.row.role == 1">Person User</span>
          <span v-else-if="scope.row.role == 2">Administrators</span>
          <span v-else-if="scope.row.role == 3">Super Admin</span>
        </template>
      </el-table-column>
      <el-table-column prop="longitude" label="longitude" width="150">
      </el-table-column>
      <el-table-column prop="latitude" label="latitude" width="150">
      </el-table-column>
      <el-table-column prop="point.person" label="personal points" width="130">
      </el-table-column>
      <el-table-column prop="point.share" label="share points" width="110">
      </el-table-column>
      <el-table-column prop="last_login" label="last_login">
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { GetUsersAPI, ApplyAdminAPI } from '@/api'
export default {
  data() {
    return {
      tableData: [],

      form: {
        role: 1,
        status: 2
      }
    }
  },
  methods: {
    // Query all users by form
    async getUsers() {
      let { data } = await GetUsersAPI(this.form)
      this.tableData = data.users
    },
    // Approval administrator application
    async applyAdmin(scope, status) {
      let uid = scope.row.uid
      let { data } = await ApplyAdminAPI({ uid, status })
      if (data.code == 0) {
        this.tableData.splice(scope.$index, 1)
      }
    }
  },
  mounted() {
    this.getUsers()
  }
}
</script>
<style scoped></style>